<template>
  <van-form @submit="onSubmit">
      <van-field
          v-model="editUser.currentValue"
          :name="editUser.editKey"
          :label="editUser.editName"
          :placeholder="`请输入${editUser.editName}`"
      />
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup lang="ts">
import {useRoute,useRouter} from "vue-router";
import {onMounted, ref} from "vue"
import myAxios from "../plugins/myAxios.ts";
import {Toast} from "vant";
import {getCurrentUser} from "../services/user";
//import router from "../config/route.ts";

const route=useRoute();
const router=useRouter();

const editUser=ref({
  editKey: route.query.editKey,
  currentValue: route.query.currentValue,
  editName: route.query.editName,
})

interface ApiResponse {
  code: number;
  data: any;
}

//2.28号更改
// const onSubmit = async () => {
//   try {
//     const res: ApiResponse = await myAxios.post('/user/update', {
//       'id': 1,
//       [editUser.value.editKey as string]: editUser.value.currentValue
//     });
//     console.log(res, '更新请求');
//     if (res.code === 0 && res.data > 0) {
//       Toast.success('修改成功');
//       router.back();
//     } else {
//       Toast.fail('修改错误');
//     }
//   } catch (error) {
//     console.error(error, '请求错误');
//     Toast.fail('请求失败');
//   }
// };



const onSubmit=async()=>{
  //获取用户信息
  const currentUser= await getCurrentUser();

  if (!currentUser){
    Toast.fail("用户未登录");
    return;
  }
  console.log(currentUser,'当前用户')

  const res=await myAxios.post('/user/update',{
    'id':currentUser.id,
    [editUser.value.editKey]:editUser.value.currentValue
  }) as ApiResponse;
  console.log(res,'更新请求')
  if (res.code === 0 && res.data > 0){
    Toast.success('修改成功');
    router.back();

  } else {
    Toast.fail('修改错误');
  }
};
</script>

<style scoped>

</style>







